package com.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javabean.Role;
import com.javabean.Users;
import com.service.RoleService;
import com.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author: 张波
 * @Time: 2025/8/28 14:04
 */
@Controller
@RequestMapping("/users")
public class UsersController {
	final UsersService usersService;
	final RoleService roleService;
	@Autowired
	public UsersController(UsersService usersService, RoleService roleService) {
		this.usersService = usersService;
		this.roleService = roleService;
	}
	@RequestMapping(value = "/login",method = RequestMethod.GET)
	public String login(HttpServletRequest request){
		Object users = request.getSession().getAttribute("users");
		if (users!=null){
			return "main";
		}
		return "login";
	}
	@RequestMapping(value = "/login",method = RequestMethod.POST)
	public String login(HttpServletRequest request,String username,String password){
		Users login = usersService.Login(username, password);
		if (login!=null){
			request.getSession().setAttribute("users",login);
			return "main";
		}else {
			request.setAttribute("msg","账号或密码错误");
		}
		return "login";
	}
	@RequestMapping("/logout")
	public String logout(HttpServletRequest request){
		request.getSession().invalidate();
		return "/login";
	}
	@RequestMapping("/findAll")
	public String findAll(HttpServletRequest request, @RequestParam(value = "page",defaultValue = "1") int page, @RequestParam(value = "limit",defaultValue = "5")  int limit){
		PageHelper.startPage(page,limit);
		List<Users> all = usersService.findAll();
		PageInfo<Users> usersPageInfo = new PageInfo<>(all);
		request.setAttribute("pageInfo",usersPageInfo);
		return "/users/users-list";
	}
	@RequestMapping("/delete")
	public String deleteById(int id){
		usersService.delById(id);
		return "redirect:findAll";
	}
	@RequestMapping(value = "/add",method = RequestMethod.GET)
	public String addUsers(HttpServletRequest request){
		List<Role> all = roleService.findAll();
		request.setAttribute("roleList",all);
		return "/users/users-add";
	}
	@RequestMapping(value = "/add",method = RequestMethod.POST)
	public String addUsers(Users users,int roleId){
		usersService.add(users,roleId);
		return "redirect:findAll";
	}
	@RequestMapping(value = "/edit",method = RequestMethod.GET)
	public String edit(HttpServletRequest request,int id){
		Users users = usersService.findById(id);
		List<Role> all = roleService.findAll();
		request.setAttribute("roleList", all);
		request.setAttribute("users", users);
		return "/users/users-edit";
	}

	@RequestMapping(value = "/edit",method = RequestMethod.POST)
	public String edit(Users users,int roleId){
		usersService.edit(users, roleId);
		return "redirect:findAll";
	}
}
